package com.lwp.i18n.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.*;

public class I18nUtils {

    /** 文件缓冲大小*/
    public static final int BUFF_SIZE = 8192;

    /**
     * 获取制定后缀的文件
     * @param sourcePath
     */
    /*public static List<File> getFiles(String sourcePath) {
        File sourceFile = new File(sourcePath);
        List<File> files = new ArrayList<File>();
        getExtensionFiles(sourceFile, files);
        return files;
    }*/

//    recursive

    /**
     * 获取 sourcePath 下指定后缀的文件集合，会递归所有目录
     * @param sourcePath 原始路径
     * @param fileExtName  文件扩展名 .js .java等,文件名转小写后匹配
     * @return
     */
    public static List<File> getFiles(String sourcePath, String... fileExtName) {
        List<File> files = new ArrayList<File>();
        if (StringUtils.isEmpty(sourcePath)) {
            return files;
        }

        File sourceFile = new File(sourcePath);

        if (sourceFile.isDirectory()) {
            getExtensionFiles(sourceFile, files, fileExtName);
        } else {
            addFile(files, sourceFile, fileExtName);
        }
        return files;
    }

    private static void getExtensionFiles(File path, List<File> fileList, String... fileExtName) {
        // 目录不存在文件返回空, 目录存在没有文件返回长度为0的文件数组
        File[] files = path.listFiles();
        if (files == null) {
            return;
        }
        for (File file : files) {
            if (file.isDirectory() && !file.isHidden()) {
                // 递归
                getExtensionFiles(file, fileList, fileExtName);
            } else {
                addFile(fileList, file, fileExtName);
            }
        }
    }

    private static void addFile(List<File> fileList, File file, String[] fileExtName) {
        String lowerCaseFileName = file.getName().toLowerCase();
        for (int i = 0; i < fileExtName.length; i++) {
            if (lowerCaseFileName.endsWith(fileExtName[i])
            ) {
                fileList.add(file);
            }
        }
    }

    public static String file2String(File file) {
        return file2String(file, "UTF-8", BUFF_SIZE);
    }

    public static String file2String(File file, String encoding) {
        return file2String(file, encoding, BUFF_SIZE);
    }

    public static String file2String(File file, String encoding, int buffSize) {
        StringWriter writer = new StringWriter();
        try {
            InputStreamReader reader = new InputStreamReader(new FileInputStream(file), encoding);
            char[] buffer = new char[buffSize];
            int n;
            while (-1 != (n = reader.read(buffer))) {
                writer.write(buffer, 0, n);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return writer.toString();
    }

    public static void main(String[] args) {
        String filePath = "E:\\gitee\\i18n-convert\\demo";
//        List<File> files = getFiles(filePath);

//        new File(filePath).list()
        List<File> files = getFiles(filePath, ".js");
        files.stream().forEach(file -> {
            System.out.println(file.getAbsolutePath());
//            System.out.println(file.getPath());
            System.out.println(file.getName());
        });

    }

}
